package euler.p051_100;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import euler.MainEuler;
import euler.helper.Utils;

public class Euler099 extends MainEuler {

    /*
        Comparing two numbers written in index form like
        2^11 and 3^7 is not difficult, as any calculator
        would confirm that 2^11 = 2048 < 3^7 = 2187.

        However, confirming that 632382^518061 > 519432^525806
        would be much more difficult, as both numbers contain over three million digits.

        Using http://projecteuler.net/project/base_exp.txt,
        a 22K text file containing one thousand lines with a
        base/exponent pair on each line, determine
        which line number has the greatest numerical value.

        NOTE: The first two lines in the file represent
        the numbers in the example given above.
     */

    /*
        a^x > b^y (with a,b,x,y>0) <=> log(a^x) > log(b^y) <=> x log(a) > y log(b)
     */
    public String resolve() throws IOException {
        String filePath = Utils.pathToFiles + "base_exp.txt";
        String strLine;
        int line = 0;
        int mejorLine = 0;
        int mejorExp = 0;
        int mejorBase = 1;
        BufferedReader br = null;

        try {
            br = new BufferedReader(new FileReader(new File(filePath)));

            while ((strLine = br.readLine()) != null)   {
                line++;

                String[] numeros = strLine.split(",");

                int nuevoBase = Integer.valueOf(numeros[0]);
                int nuevoExp = Integer.valueOf(numeros[1]);

                if ((nuevoBase > mejorBase && nuevoExp > mejorExp) || (nuevoExp*Math.log(nuevoBase) > mejorExp*Math.log(mejorBase))) {
                    mejorExp = nuevoExp;
                    mejorBase = nuevoBase;
                    mejorLine = line;
                }
            }

            return String.valueOf(mejorLine);
            //709
        } finally {
            if (br != null) {
                br.close();
            }
        }
    }



}
